
package com.idealighter.game.games.shuihu;

import com.idealighter.game.core.common.Game;
import com.idealighter.game.dictionary.domain.RobotConfigDomain;
import com.idealighter.game.dictionary.domain.ShuihuRoomDomain;
import com.idealighter.game.gamehall.dto.MemInfo;
import com.idealighter.game.gamehall.dto.SeatInfo;
import com.idealighter.game.games.shuihu.message.ReqEnterGameHallMsg;
import com.idealighter.game.games.shuihu.message.ReqEnterRoomMsg;
import com.idealighter.game.games.shuihu.message.ReqEnterTableMsg;
import com.idealighter.game.games.shuihu.message.ReqShuiHuStartMsg;
import com.idealighter.game.robot.core.Player;
import com.idealighter.utils.code.RandCodeUtil;

/**
 * 水浒机器人.
 *
 */
public class ShuiHuPlayer extends Player {

  public ShuihuRoomDomain roomDomain;

  /**
   * 构造函数.
   * 
   * @param playerId .
   * @param userName .
   * @param roomId .
   * @param initGold .
   * @param totalGameNum .
   * @param tableGameNum .
   */
  public ShuiHuPlayer(long playerId, String userName, int roomId, int initGold, int totalGameNum,
      int tableGameNum, RobotConfigDomain configDomain, ShuihuRoomDomain roomDomain) {
    super(playerId, userName, roomId, initGold, totalGameNum, tableGameNum, configDomain);
    this.roomDomain = roomDomain;
  }

  @Override
  public boolean goldCheck() {
    long gold = playerInfo.getGold();
    if (gold < roomDomain.getLower()) {
      LOG.info("[水浒传]机器人[{}][{}]金币小于房间下限退出游戏", userName, playerId);
      delayLogout();
      return false;
    }

    if (gold > roomDomain.getUpper()) {
      LOG.info("[水浒传]机器人[{}][{}]金币高于房间下限退出游戏", userName, playerId);
      delayLogout();
      return false;
    }

    if (gold < roomDomain.getAfee()) {
      LOG.info("[水浒传]机器人[{}][{}]金币小于台费退出游戏", userName, playerId);
      delayLogout();
      return false;
    }
    return true;
  }

  @Override
  public void enterGameHall() {
    sendMsg(new ReqEnterGameHallMsg());
  }

  @Override
  public void enterRoom() {
    if (rooms.contains(roomId)) {
      if (!goldCheck()) {
        return;
      }
      ReqEnterRoomMsg msg = new ReqEnterRoomMsg();
      msg.setRoomId(roomId);
      sendMsg(msg);
    }
  }

  @Override
  public void enterTable() {
    if (!goldCheck()) {
      return;
    }
    SeatInfo emptySeat = findEmptySeat();
    if (emptySeat != null) {
      ReqEnterTableMsg msg = new ReqEnterTableMsg();
      msg.setTableId((int) emptySeat.getTableId());
      sendMsg(msg);
    }
  }

  @Override
  public Game game() {
    return Game.SHUI_HU_ZHUAN;
  }

  /**
   * 开始水浒，筹码足够时全压，每条线压随机压,不够则退出游戏.
   */
  public void startShuiHu() {
    MemInfo member = member(playerId);
    long bet = RandCodeUtil.random(1, 20);
    long totalBet = 9 * bet;

    if (member.getChips() >= totalBet) {
      ReqShuiHuStartMsg msg = new ReqShuiHuStartMsg();
      msg.setBet(bet);
      msg.setLine(9);
      sendMsg(msg);
    } else {
      delayLogout();
    }
  }

  @Override
  public boolean enterPwdTableAble() {
    return true;
  }

}
